<?php
add_action('widgets_init', 'register_top_store_view_widget');

function register_top_store_view_widget()
{
	register_widget('top_store_view');
}
class top_store_view extends WP_Widget // widget class
{
	function top_store_view() // widget setting | class name must be same function name
	{
		$widget_ops = array('classname' => 'top-store-view-w', 'description' =>
				'Display top view Stores');
		$control_ops = array(
			'width' => 250,
			'height' => 350,
			'id_base' => 'top_store_view');
		$this->WP_Widget('top_store_view', 'CouponX - Top view Stores', $widget_ops, $control_ops);
	}
	function widget($args, $instance) // display widget
	{
		extract($args);
        $title = $instance['title'];
        $kw = ' '.$instance['kw'];
        $number_show = $instance['number_show'];
        if(!$number_show)
            $number_show = 10;
        $show_as = $instance['show_as'];
        $get_by = $instance['get_by'];
        
        if($get_by == 'random')
            $arr_top_st_ids = cpx_get_top_view_stores($number_show, $get_by);
        else
            $arr_top_st_ids = cpx_get_top_view_stores($number_show);
        
        if(count($arr_top_st_ids) > 0)
        {
        ?>
        <div class="row-fluid topstoresxx" style="margin-top: 10px;padding-bottom:10px">
                    <div class="titlestopxx"><?php echo $title; ?></div>
        <?php if($show_as == 'link'): ?>
                    <ul class="peoplewho">
                    <?php foreach ($arr_top_st_ids as $id): ?>
                        <li><a href="<?php echo get_permalink($id); ?>" title="Get <?php echo get_post_field('post_title',$id) . $kw; ?>"><?php echo get_post_field('post_title',$id) . $kw; ?></a></li>
                    <?php endforeach; ?>                    
                    </ul>
        <?php else: ?>
            <?php  
            foreach ($arr_top_st_ids as $id):
                $st_home_url = get_post_meta($id, 'url_store_metadata', true);
                $src_img = get_post_meta($id, 'logo_metadata', true);
                if(!$src_img)
                    $src_img = cpx_get_store_thumb($st_home_url, 100, 50);
            ?>
            <div class="group-top-stores">
                <div class="top-store">
                <a href="<?php echo get_permalink($id);?>" title="Get <?php echo get_post_field('post_title',$id) . $kw; ?>"><img src="<?php echo $src_img; ?>" width="50px" height="50px" /></a>
                </div>
            
            <span><a href="<?php echo get_permalink($id);?>" title="Get <?php echo get_post_field('post_title',$id) . $kw; ?>"><?php echo get_post_field('post_title', $id) . $kw; ?></a></span>
            </div>
            <?php
            endforeach;
            ?>
        <?php endif; ?>
        </div>    
    <?php
        }
	}
    
	function update($new_instance, $old_instance) // update widget
	{
		$instance = $old_instance;		
        $instance['title'] = $new_instance['title'];
        $instance['kw'] = $new_instance['kw'];
        $instance['number_show'] = $new_instance['number_show'];
        $instance['show_as'] = $new_instance['show_as'];
        $instance['get_by'] = $new_instance['get_by'];
		return $instance;
	}
    
	function form($instance) // form for the widget options
	{
	   $arr_show_as = array('link' => 'Links','img' => 'Store image');
       $arr_get_store_by = array('top_view' => 'Top view','random' => 'Random');
?>
        <div style="color: #333;">
    		<p>        
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
            <p>        
    			<label for="<?php echo $this->get_field_id('kw');?>"><?php echo 'Keyword after store name'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('kw'); ?>" name="<?php	echo $this->get_field_name('kw');?>" value="<?php echo $instance['kw'];?>" style="width:100%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('number_show');?>"><?php echo 'Number of stores to show (default 10)';?></label>
    			<input type="text" id="<?php echo $this->get_field_id('number_show');?>" name="<?php echo $this->get_field_name('number_show');?>" value="<?php echo $instance['number_show'];?>" style="width:20%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('get_by');?>"><?php echo 'Get stores by';?></label>
                <select name="<?php echo $this->get_field_name('get_by'); ?>" id="<?php echo $this->get_field_id('get_by') ?>" style="width: 60%;">
                <?php  
                foreach ($arr_get_store_by as $k => $v) {
                    if ($instance['get_by'] == $k)
                        $selected = 'selected';
                    else
                        $selected = '';
                    echo "<option value='{$k}' {$selected}>$v</option>";
                }
                ?>
                </select>
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('show_as');?>"><?php echo 'Show as';?></label>
                <select name="<?php echo $this->get_field_name('show_as'); ?>" id="<?php echo $this->get_field_id('show_as') ?>" style="width: 60%;">
                <?php  
                foreach ($arr_show_as as $k => $v) {
                    if ($instance['show_as'] == $k)
                        $selected = 'selected';
                    else
                        $selected = '';
                    echo "<option value='{$k}' {$selected}>$v</option>";
                }
                ?>
                </select>
    		</p>
        </div>
<?php
	}
}
?>
